// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <nebula/types/meta/list_type.h>

namespace nebula {


    /// \brief Concrete type class for map data
    ///
    /// Map data is nested data where each value is a variable number of
    /// key-item pairs.  Its physical representation is the same as
    /// a list of `{key, item}` structs.
    ///
    /// Maps can be recursively nested, for example map(utf8, map(utf8, int32)).
    class TURBO_EXPORT MapType : public ListType {
    public:
        static constexpr Type::type type_id = Type::MAP;

        static constexpr const char *type_name() { return "map"; }

        MapType(std::shared_ptr<DataType> key_type, std::shared_ptr<DataType> item_type,
                bool keys_sorted = false);

        MapType(std::shared_ptr<DataType> key_type, std::shared_ptr<Field> item_field,
                bool keys_sorted = false);

        MapType(std::shared_ptr<Field> key_field, std::shared_ptr<Field> item_field,
                bool keys_sorted = false);

        explicit MapType(std::shared_ptr<Field> value_field, bool keys_sorted = false);

        // Validating constructor
        static turbo::Result<std::shared_ptr<DataType>> create(std::shared_ptr<Field> value_field,
                                                               bool keys_sorted = false);

        std::shared_ptr<Field> key_field() const { return get_value_type()->field(0); }

        std::shared_ptr<DataType> key_type() const { return key_field()->type(); }

        std::shared_ptr<Field> item_field() const { return get_value_type()->field(1); }

        std::shared_ptr<DataType> item_type() const { return item_field()->type(); }

        std::string to_string(bool show_metadata = false) const override;

        std::string name() const override { return "map"; }

        bool keys_sorted() const { return keys_sorted_; }

    private:
        std::string compute_fingerprint() const override;

        bool keys_sorted_;
    };
}  // namespace nebula
